def count_char_frequency(s):
    """统计字符串中每个字符的出现次数"""
    freq = {}
    for char in s:
        # 若字符已在字典中，计数+1；否则初始化为1
        freq[char] = freq.get(char, 0) + 1
    return freq

# 测试
if __name__ == "__main__":
    s = "hello world"
    print(count_char_frequency(s))
    # {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}